//
//  BIDBaseForPublishViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/12.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDBaseForPublishViewController.h"

@interface BIDBaseForPublishViewController ()<BIDAddPhotoViewDelegate, ELCImagePickerControllerDelegate, UINavigationControllerDelegate, BIDUploadFileDelegate>
{

}
@end

@implementation BIDBaseForPublishViewController
@synthesize maxImgCount;
@synthesize publishRequestURL;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    maxImgCount = 3;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(closeHandler)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 初始化基本数据
 */
- (void)prepareData
{
    _addPhotoView.delegate = self;
    //
    _uploadFile = [[BIDUploadFile alloc] initWithURL:self.publishRequestURL];
    _uploadFile.delegate = self;
    //
}

- (void)closeHandler
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (void)alertViewHandler
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

#pragma mark - BIDAddPhotoViewDelegate
- (void)choosePhoto
{
    if(_addPhotoView.imgArr.count == maxImgCount)
    {
        NSString *strHint = [[NSString alloc] initWithFormat:@"最多选择%ld张图片", maxImgCount];
        [self showAlertView:strHint buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    //
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
    
    //3-_addPhotoView.imgArr.count
    elcPicker.maximumImagesCount = 111; //Set the maximum number of images to select to 100
    elcPicker.returnsOriginalImage = YES; //Only return the fullScreenImage, not the fullResolutionImage
    elcPicker.returnsImage = YES; //Return UIimage if YES. If NO, only return asset location information
    elcPicker.onOrder = YES; //For multiple image selection, display and return order of selected images
    elcPicker.mediaTypes = @[(NSString *)kUTTypeImage]; //Supports image and movie types((NSString *)kUTTypeMovie)
    
    elcPicker.imagePickerDelegate = self;
    
    [self presentViewController:elcPicker animated:YES completion:nil];
}

- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray *)arr
{
    BIDBrowserImageViewController *vc = [[BIDBrowserImageViewController alloc] init];
    vc.index = index;
    vc.imgArr = arr;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)resizeViewWithFrame:(CGRect)frame
{
    _heightForAddPhotoViewConstraint.constant = CGRectGetHeight(frame);
    _heightForContainerViewConstraint.constant = CGRectGetMaxY(frame) + 8;//CGRectGetMinY(_addPhotoView.frame)+CGRectGetHeight(frame)+8;
    //[self.view layoutIfNeeded];
    CGSize contentSize = _scrollView.contentSize;
    contentSize.height = CGRectGetHeight(_containerView.frame);
    [_scrollView setContentSize:contentSize];
}

#pragma mark - BIDUpladFileDelegate
- (void)finishUploadWithResult:(id)obj
{
    if([obj isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dic = (NSDictionary *)obj;
        NSNumber *value = dic[@"head"][@"success"];
        if([value intValue] == 1)
        {
            [self showAlertView:@"发布成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler"];
        }
        else
        {
            NSString *strErrMsg = @"";
            if(dic[@"head"][@"msg"])
            {
                strErrMsg = dic[@"head"][@"msg"];
            }
            else
            {
                strErrMsg = @"发布失败";
            }
            [self showAlertView:strErrMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
    }
}

- (void)uploadFailedWithError:(NSError *)err
{
    [self showAlertView:[err localizedDescription] buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
}

#pragma mark - ELCImagePickerControllerDelegate Methods

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
    for (NSDictionary *dict in info)
    {
        if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto)
        {
            if ([dict objectForKey:UIImagePickerControllerOriginalImage])
            {
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
                [images addObject:image];
                [_addPhotoView addPhoto:image];
            }
            else
            {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        }
        else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo)
        {
            if ([dict objectForKey:UIImagePickerControllerOriginalImage])
            {
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
                [images addObject:image];
            }
            else
            {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        }
        else
        {
            NSLog(@"Uknown asset type");
        }
    }
    //_chosenImgArr = images;
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
